package com.cmcc.pcap.packet.tcp.https.xdr; /**
 *
 */
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.cmcc.packet.entity.XdrEntity;
import com.cmcc.pcap.packet.tcp.TcpPacketDecorator;
import com.cmcc.pcap.packet.tcp.http.HttpTransaction;
import com.cmcc.pcap.packet.util.DateUtil;


/**
 * @Date 2018年3月21日 下午6:48:11
 * @author Amish
 *
 */
public class HttpsXdrGenerate {
    private static Logger logger = LogManager.getLogger(HttpsXdrGenerate.class);

//    public  ArrayList<XdrEntity> xdrhttpsEntityList = new ArrayList<XdrEntity>();

    public List<XdrEntity> generate(HttpTransaction httpsTransaction,String taskId) {
    	ArrayList<XdrEntity> xdrhttpsEntityList = new ArrayList<XdrEntity>();
        //TcpConnection tcpConnection = httpTransaction.tcpConnection;
        XdrEntity xdrhttpsEntity = new XdrEntity();
        xdrhttpsEntity.setType("HTTPS");
        
        StringBuilder server_name = null;
        Iterator<TcpPacketDecorator> i = httpsTransaction.al_splited.iterator();
        while(i.hasNext()){
        	
        	xdrhttpsEntity.setTaskId(Integer.valueOf(taskId));
			
			//根据taskid查询apptypefeature，appsubtypefeature，appcontentfeature
//        	XdrDataInfoService xdrDataInfoService = ProtocolSpringContextUtils.getBean("xdrDataInfoServiceImpl");
//        	XdrDataInfoEntity xdrDataInfoEntity = xdrDataInfoService.getXdrDataInfoById(Long.parseLong(taskId));
//        	String appTypeCode = xdrDataInfoEntity.getAppTypeCode();
//        	String appSubTypeCode = xdrDataInfoEntity.getAppSubTypeCode();
//        	String appContentCode = xdrDataInfoEntity.getAppContentCode();
//        	xdrhttpsEntity.setAppTypeCd(appTypeCode);
//        	xdrhttpsEntity.setAppSubTypeCd(appSubTypeCode);
//        	xdrhttpsEntity.setAppContentCd(appContentCode);
        	
            TcpPacketDecorator tpd = (TcpPacketDecorator) i.next();
            if(tpd.tcpPacket.getPayload() != null && tpd.tcpPacket.getPayload().getRawData().length > 0){
                //获取 server name
                StringBuilder server_name_temp = findHttpsClientHelloInfo2(tpd);
                if(server_name_temp != null){
                    server_name = server_name_temp;
                    break;
                }
            }
        }
        String server_name_final = ""+server_name;
        xdrhttpsEntity.setServerName(server_name_final);
        logger.debug("server name is: "+server_name_final);


        String id_at_commonName = null;
        Iterator<TcpPacketDecorator> j = httpsTransaction.al_splited.iterator();
        while(j.hasNext()){
            TcpPacketDecorator tpd = (TcpPacketDecorator) j.next();
            if(tpd.tcpPacket.getPayload() != null && tpd.tcpPacket.getPayload().getRawData().length > 0){
                //获取 server name
                String id_at_commonName_temp = findHttpsCertificateInfo(tpd);
                if(id_at_commonName_temp != null && id_at_commonName_temp.length()>0){
                    id_at_commonName = id_at_commonName_temp;
                    break;
                }
            }
        }
        String id_at_commonName_final = id_at_commonName;
        xdrhttpsEntity.setIdAtCommonName(id_at_commonName_final);
        logger.debug("id at commonName is: "+id_at_commonName_final);

		//业务识别
//		Environment environment = ProtocolSpringContextUtils.getBean("environment");
//		String systemName = environment.getProperty("system.name");
//		String appDistinguishPath = environment.getProperty("file.path.root");
//
//		if("awareness".equalsIgnoreCase(systemName)) {
//			AppDistinguish appDistinguish = new AppDistinguish();
//			Map<String,Object> pathMap = appDistinguish.getAppDistinguishPaths(appDistinguishPath);
//			
//    		TaskAwarenessService taskAwarenessService = ProtocolSpringContextUtils.getBean("taskAwarenessServiceImpl");
//    		TaskAwarenessEntity entity = taskAwarenessService.getTaskAwarenessById(Long.parseLong(taskId));
//    		List<String> httpProtoList = entity.getRuleFileList();
//    		
//			if(entity.getIsUseGroupRule()==1) {//需要使用集团规则库文件
//				List<String> groupProtoList = (List<String>)pathMap.get("httpprotoList");
//				if(!Collections.isEmpty(groupProtoList)) {
//					httpProtoList.addAll(groupProtoList);
//				}
//			}
//			Map<String,String> map = appDistinguish.httpsAppDistinguishPcap4j(server_name_final, id_at_commonName_final, (String)pathMap.get("protolistPath"),httpProtoList);
//			if(map!=null) {
//				xdrhttpsEntity.setAppTypeCd(map.get("appType"));
//				xdrhttpsEntity.setAppSubTypeCd(map.get("appType")+""+map.get("appSubType"));
//			}
//		}
        try {
            saveHttpsTcpConnectionToxdrhttpsEntityList(httpsTransaction,xdrhttpsEntity);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            logger.error(e.getCause(), e);
        }

        //将每个XdrEntity加入XdrEntityList中，调用SaveHttpTransactionInfo的save方法保存到数据库中
        xdrhttpsEntityList.add(xdrhttpsEntity);

        /**
         * Interface
         * ProcedureStartTime
         * ProcedureEndTime
         * ProtocolType
         * AppType
         * AppSubType
         * IPAddressType
         * UserIPv4
         * UserIPv6
         * ......
         *
         * UL Data上行流量
         * DL Data下行流量
         * UL IP Packet上行IP包数
         * DL IP Packet下行IP包数
         * UL Disorder IP Packet上行TCP乱序报文数
         * DL Disorder IP Packet下行TCP乱序报文数
         * UL Retrans IP Packet上行TCP重传报文数
         * DL Retrans IP Packet下行TCP重传报文数
         * UL_IP_FRAG_PACKETS上行IP包的分片数
         * DL_IP_FRAG_PACKETS下行IP包的分片数
         * TCP SYN Num：TCP SYN次数
         * TCP SYN ACK Num：TCP SYN ACK的次数
         * TCP ACK Num：回复SYN ACK的ACK次数
         *
         *
         */
        return xdrhttpsEntityList;
    }

    public void saveHttpsTcpConnectionToxdrhttpsEntityList(HttpTransaction httpsTransaction,XdrEntity xdrhttpsEntity) throws ParseException{

    	if(httpsTransaction.al_splited==null||httpsTransaction.al_splited.size()==0)
			return;
        
    	//填写IE和PORTAL,默认值是0，必须填写
        xdrhttpsEntity.setIe(0);
        xdrhttpsEntity.setPortal(0);

        Integer length = httpsTransaction.al_splited.size();
        logger.debug("tcpConnection length:"+length);

        TcpPacketDecorator tpdStart = httpsTransaction.al_splited.get(0);
        TcpPacketDecorator tpdEnd = httpsTransaction.al_splited.get(length-1);

        //计算procedure_start_time
        long timestamp_procedure_start_time = tpdStart.timestamp.getTime();
        Date procedureStartTime = DateUtil.formatTimestamp(timestamp_procedure_start_time);
        xdrhttpsEntity.setProcedureStartTime(procedureStartTime);

        //计算procedure_end_time
        long timestamp_procedure_end_time = tpdEnd.timestamp.getTime();
        Date procedureEndTime = DateUtil.formatTimestamp(timestamp_procedure_end_time);
        xdrhttpsEntity.setProcedureEndTime(procedureEndTime);

        //填写interface
        Integer interFace = 11;
        xdrhttpsEntity.setInterfaceCode(interFace);      
        
        //找到连接的connectionKey直接填写源地址和目的地址
        String[] connectionKey = httpsTransaction.tcpConnection.connectionKey.split("-");
        String[] usersocket = connectionKey[0].split(":");
        String[] serversocket = connectionKey[1].split(":");

        String userIpv4;
        String userIpv6;
        Integer userPort;
        
        String appServerIpIpv4;
        String appServerIpIpv6;
        Integer appServerPort;
        
        if(usersocket.length>2) {
        	//填写终端用户的ipv6地址和端口
        	String[] useriplist = Arrays.copyOf(usersocket, usersocket.length-1);
        	userIpv6 = String.join(":", useriplist);
        	userPort =  Integer.valueOf(usersocket[8]);
        	
        	xdrhttpsEntity.setUserIpv6(userIpv6);
        	xdrhttpsEntity.setUserPort(userPort);
            
        	// 填写ip_address_type,ipv4是0,ipv6是1
        	xdrhttpsEntity.setIpAddressType(1);

        }
        else {
        	//填写终端用户的ipv4地址和端口
        	userIpv4 = usersocket[0];
        	userIpv4 = userIpv4.replace("/", "");
        	userPort = Integer.valueOf(usersocket[1]);
        	
        	xdrhttpsEntity.setUserIpv4(userIpv4);
        	xdrhttpsEntity.setUserPort(userPort);
            
        	// 填写ip_address_type,ipv4是0,ipv6是1
        	xdrhttpsEntity.setIpAddressType(0);

        }


        // 填写L4 Protocol,0为tcp,1为udp
        xdrhttpsEntity.setL4Protocal(0);

        if(serversocket.length>2) {
        	//填写服务端用户的ipv6地址和端口
        	String[] appserveriplist = Arrays.copyOf(serversocket, serversocket.length-1);
        	appServerIpIpv6 = String.join(":", appserveriplist);
        	appServerPort =  Integer.valueOf(serversocket[8]);
        	
        	xdrhttpsEntity.setAppServerIpIpv6(appServerIpIpv6);
        	xdrhttpsEntity.setAppServerPort(appServerPort);
        }
        else {
        	//填写服务端用户的ipv4地址和端口
        	appServerIpIpv4 = serversocket[0];
        	appServerIpIpv4 = appServerIpIpv4.replace("/", "");
        	appServerPort = Integer.valueOf(serversocket[1]);
        	
        	xdrhttpsEntity.setAppServerIpIpv4(appServerIpIpv4);
        	xdrhttpsEntity.setAppServerPort(appServerPort);
        }

        
        //填写上行流量DL DATA
        Integer ulData = httpsTransaction.UL_Data;
        xdrhttpsEntity.setUlData(ulData);

        //填写下行流量UL DATA
        Integer dlData = httpsTransaction.DL_Data;
        xdrhttpsEntity.setDlData(dlData);

        //填写上行ip包个数
        Integer ulIpPacket = httpsTransaction.UL_IP_Packet;
        xdrhttpsEntity.setUlIpPacket(ulIpPacket);

        //填写下行ip包葛素
        Integer dlIpPacket = httpsTransaction.DL_IP_Packet;
        xdrhttpsEntity.setDlIpPacket(dlIpPacket);

        //填写上行tcp乱序包
        Integer ulDisorderIpPacket = httpsTransaction.UL_Disorder_IP_Packet;
        xdrhttpsEntity.setUlDisorderIpPacket(ulDisorderIpPacket);

        //填写下行tcp乱序包
        Integer dlDisorderIpPacket = httpsTransaction.DL_Disorder_IP_Packet;
        xdrhttpsEntity.setDlDisorderIpPacket(dlDisorderIpPacket);

        //填写上行tcp重传包
        Integer ulRetransIpPacket = httpsTransaction.UL_Retrans_IP_Packet;
        xdrhttpsEntity.setUlRetransIpPacket(ulRetransIpPacket);

        //填写下行tcp重传包
        Integer dlRetransIpPacket = httpsTransaction.DL_Retrans_IP_Packet;
        xdrhttpsEntity.setDlRetransIpPacket(dlRetransIpPacket);

        //填写上行ip分片数
        Integer ulIpFragPackets = httpsTransaction.UL_IP_FRAG_PACKETS;
        xdrhttpsEntity.setUlIpFragPackets(ulIpFragPackets);

        //填写下行ip分片数
        Integer dlIpFragPackets = httpsTransaction.DL_IP_FRAG_PACKETS;
        xdrhttpsEntity.setDlIpFragPackets(dlIpFragPackets);

        //填写tcp中syn的次数
        Integer tcpSynNum = httpsTransaction.TCP_SYN_Num;
        xdrhttpsEntity.setTcpSynNum(tcpSynNum);

        //填写tcp中syn ack次数
        Integer tcpSynAckNum = httpsTransaction.TCP_SYN_ACK_Num;
        xdrhttpsEntity.setTcpSynAckNum(tcpSynAckNum);

        //填写回复syn ack的ack次数
        Integer tcpAckNum = httpsTransaction.TCP_ACK_Num;
        xdrhttpsEntity.setTcpAckNum(tcpAckNum);

        //填写protocol type，https是2
        xdrhttpsEntity.setProtocolType("2");


    }

    public String findHttpsCertificateInfo(TcpPacketDecorator tcpPacketDecorator){
//
//		byte[] tslhandshake1 = {(byte) 0x16};//记录层握手协议类型标识
//		byte[] tslhandshake2 = {(byte) 0x0b};//上层协议中的握手协议标识,certificate为Ox0b

        if(tcpPacketDecorator.tcpPacket.getRawData().length == 0){//该判断多余0613add
            return null;
        }

//		byte[] SSLhandshake1 = new byte[1];
//		byte[] SSLhandshake2 = new byte[1];
        byte[] data = tcpPacketDecorator.getData();//TODO https 由于不知道id-at-commonName字段具体会出现在第几个分段，暂时采用获取所有分段。性能问题?
        for (int i = 0; i < data.length-3; i++) {
            if (data[i] == 85 && data[i+1] == 4 && data[i+2] == 3){
                byte[] bytes = new byte[data[i + 4]];
                System.arraycopy(data,i+5, bytes,0, data[i + 4]);
                String idAtCommentName = new String(bytes, Charset.forName("utf-8"));
                boolean matches = Pattern.matches(".*\\.?.*\\..*", idAtCommentName);//TODO 正则匹配?穷举域名后缀?
                if (matches){
                    return idAtCommentName;
                }
            }
        }

        return null;

		/*System.arraycopy(data, 0, SSLhandshake1, 0, 1);//记录层握手协议类型为数据部分第1个字节
		System.arraycopy(data, 5, SSLhandshake2, 0, 1);//上层协议的握手协议标识为数据部分的第6个字节

		if(Arrays.equals(SSLhandshake1, tslhandshake1) && Arrays.equals(SSLhandshake2, tslhandshake2)){
			logger.info("-------------SSL-----Certificate");
			String dataString = printHexBinary(data);
			dataString.getBytes();
			List<String> list = new ArrayList<>();
			List<String> list1 = new ArrayList<>();
			String str[] = dataString.split(",");
			list = Arrays.asList(str);

		}*/
    }

    public StringBuilder findHttpsClientHelloInfo(TcpPacketDecorator tcpPacketDecorator){

        byte[] tslhandshake1 = {(byte) 0x16};//记录层握手协议类型标识
        byte[] tslhandshake2 = {(byte) 0x01};//上层协议中的握手协议标识，clienthello为Ox01

        if(tcpPacketDecorator.tcpPacket.getRawData().length == 0){//该判断多余0613add
            return null;
        }

        /**
         * ssl协议识别逻辑：
         * 由于TCP分段的原因，有些基于TCP连接的SSL没有握手阶段，且握手阶段可能存在client发往server的第二条信息由于乱序的原因比第一条client hello更早到达server端，
         * 而该条信息并不包括server_name字段，所以判断条件取以下三个条件的逻辑“与”：
         * 		1，端口号为“443”
         * 		2，判断记录层握手协议类型，即字段content type，该字段为0x16表示为握手协议，为0x17表示为应用数据，也即分段数据
         * 		3，判断上层协议中的握手协议标识，即字段handshake type，该字段为0x01表示为client hello，为0x02表示为server hello，
         * 		      相应的有certificate等握手阶段的对应字段值
         * 0727
         */

        byte[] SSLhandshake1 = new byte[1];
        byte[] SSLhandshake2 = new byte[1];
        byte[] data = tcpPacketDecorator.tcpPacket.getPayload().getRawData();
        System.arraycopy(data, 0, SSLhandshake1, 0, 1);//记录层握手协议类型为数据部分第1个字节
        System.arraycopy(data, 5, SSLhandshake2, 0, 1);//上层协议的握手协议标识为数据部分的第6个字节

        if(Arrays.equals(SSLhandshake1, tslhandshake1) && Arrays.equals(SSLhandshake2, tslhandshake2)){
            logger.debug("-------------SSL-----Client Hello");
            String dataString = printHexBinary(data);//
            logger.debug("-------------data + "+dataString);
            dataString.getBytes();
            List<String> list = new ArrayList<>();
            List<String> list1 = new ArrayList<>();
            String str[] = dataString.split(",");
            list = Arrays.asList(str);
            logger.debug(list.size());

            /**
             * server_name字段获取
             * 通过每个字段含义以及长度顺次寻找server_name字段位置
             *
             */
            for (int i = 0; i < list.size(); i++) {

                //第44位为session ID length字段，该字段为0时，其下一个字段为Cipher Suites Length
                if(i == 43 && "00".equalsIgnoreCase(list.get(i))){


                    i = i + 1;

                    String tmp = list.get(i)+list.get(i+1);
                    int lengthCipher = Integer.parseInt(tmp,16);
                    i = i + 2 + lengthCipher;//该字段占2字节，故加2

                    int lengthCompression = Integer.parseInt((list.get(i)),16);
                    i = i + 1 + lengthCompression;//该字段占1字节，故加1
                    
                    //lvpin 20200909 temp ：如果有renegotiation_info扩展头，会在service_name扩展头前增加5个字节？ TODO此处需要彻底完善
                    if("FF".equalsIgnoreCase(list.get(i+2))) {//匹配到renegotiation_info，实际是“FF01”
                     i = i+5;//renegotiation_info扩展头整个长度是5？
                    }

                    //数字9表示字段Compression Methods后到字段server_name length之前的长度，为固定的9字节
                    tmp = list.get(i + 9)+list.get(i + 9 + 1);
                    int lengthServerName = Integer.parseInt(tmp,16);
                    list1.addAll(list.subList(i + 9 + 2, i + 9 + 2 + lengthServerName));//该字段占2字节，故加2
                    break;
                }
                //第44位为session ID length字段，该字段不为0时，其下一个字段为session ID
                if(i == 43 && !"00".equalsIgnoreCase(list.get(i))){

                    int lengthSession = Integer.parseInt(list.get(i),16);
                    i = i + 1 + lengthSession;//该字段占1字节，故加1

                    int lengthCipher = Integer.parseInt((list.get(i)+list.get(i+1)),16);
                    i = i + 2 + lengthCipher;//该字段占2字节，故加2

                    int lengthCompression = Integer.parseInt((list.get(i)),16);
                    i = i + 1 + lengthCompression;//该字段占1字节，故加1

                    //数字9表示字段Compression Methods后到字段server_name length之前的长度，为固定的9字节
                    int lengthServerName = Integer.parseInt((list.get(i + 9)+list.get(i + 9 + 1)),16);
                    list1.addAll(list.subList(i + 9 + 2, i + 9 + 2 + lengthServerName));//该字段占2字节，故加2
                    break;

                }
            }

            logger.info(list1);

            //将该字段解析
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < list1.size(); i++){
                int decimal = Integer.parseInt(list1.get(i), 16);
                sb.append((char) decimal);
            }
            sb.toString();
            logger.info(sb);

            //logger.info("-------------ascii + "+convertHexToString(printHexBinary(data)));
            return sb;
        }

        return null;

    }
    
    /**
     * 20201208 lvpin 
     * @param tcpPacketDecorator
     * @return
     */
    public StringBuilder findHttpsClientHelloInfo2(TcpPacketDecorator tcpPacketDecorator){

        byte[] tslhandshake1 = {(byte) 0x16};//记录层握手协议类型标识
        byte[] tslhandshake2 = {(byte) 0x01};//上层协议中的握手协议标识，clienthello为Ox01

        if(tcpPacketDecorator.tcpPacket.getRawData().length == 0){//该判断多余0613add
            return null;
        }

        /**
         * ssl协议识别逻辑：
         * 由于TCP分段的原因，有些基于TCP连接的SSL没有握手阶段，且握手阶段可能存在client发往server的第二条信息由于乱序的原因比第一条client hello更早到达server端，
         * 而该条信息并不包括server_name字段，所以判断条件取以下三个条件的逻辑“与”：
         * 		1，端口号为“443”
         * 		2，判断记录层握手协议类型，即字段content type，该字段为0x16表示为握手协议，为0x17表示为应用数据，也即分段数据
         * 		3，判断上层协议中的握手协议标识，即字段handshake type，该字段为0x01表示为client hello，为0x02表示为server hello，
         * 		      相应的有certificate等握手阶段的对应字段值
         * 0727
         */

        byte[] SSLhandshake1 = new byte[1];
        byte[] SSLhandshake2 = new byte[1];
        byte[] data = tcpPacketDecorator.tcpPacket.getPayload().getRawData();
        System.arraycopy(data, 0, SSLhandshake1, 0, 1);//记录层握手协议类型为数据部分第1个字节
        System.arraycopy(data, 5, SSLhandshake2, 0, 1);//上层协议的握手协议标识为数据部分的第6个字节

        if(Arrays.equals(SSLhandshake1, tslhandshake1) && Arrays.equals(SSLhandshake2, tslhandshake2)){
            logger.info("-------------SSL-----Client Hello");
            String dataString = printHexBinary(data);//方法中会添加","
            logger.info("-------------data + "+dataString);
            dataString.getBytes();
            List<String> list = new ArrayList<>();
            List<String> list1 = new ArrayList<>();
            String str[] = dataString.split(",");
            list = Arrays.asList(str);
            logger.info(list.size());

            /**
             * server_name字段获取
             * 通过每个字段含义以及长度顺次寻找server_name字段位置
             *
             */
            for (int i = 0; i < list.size(); i++) {

                //第44位为session ID length字段，该字段为0时，其下一个字段为Cipher Suites Length
                if(i == 43 && "00".equalsIgnoreCase(list.get(i))){


                    i = i + 1;

                    String tmp = list.get(i)+list.get(i+1);
                    int lengthCipher = Integer.parseInt(tmp,16);
                    i = i + 2 + lengthCipher;//该字段占2字节，故加2

                    int lengthCompression = Integer.parseInt((list.get(i)),16);
                    i = i + 1 + lengthCompression;//该字段占1字节，故加1
                    
                    
                    //Extensions length
                    int lengthExtensions = Integer.parseInt(list.get(i)+list.get(i+1),16);
                    i = i + 2;//该字段占2个字节，故加2
                    
                    //循环解析Extensions
                    for(int j=0 ;j<lengthExtensions;j++) {
                        int extensionType = Integer.parseInt(list.get(i)+list.get(i+1),16);
                    	if(extensionType != 0) {
                    		i=i+2;
                            int extensionLength = Integer.parseInt(list.get(i)+list.get(i+1),16);
                            i = i+2+extensionLength;
                        }else {//找到service_name
                        	i=i+2;
                    		int extensionLength = Integer.parseInt(list.get(i)+list.get(i+1),16);
                    		i=i+2;
                    		list1.addAll(list.subList(i+5, i + extensionLength));//这里的5是server name list/2，service name type/1，service name length/2，字段长度之和
                    		break;
                        }
                    }
                    break;
                }
                //第44位为session ID length字段，该字段不为0时，其下一个字段为session ID
                if(i == 43 && !"00".equalsIgnoreCase(list.get(i))){

                    int lengthSession = Integer.parseInt(list.get(i),16);
                    i = i + 1 + lengthSession;//该字段占1字节，故加1

                    int lengthCipher = Integer.parseInt((list.get(i)+list.get(i+1)),16);
                    i = i + 2 + lengthCipher;//该字段占2字节，故加2

                    int lengthCompression = Integer.parseInt((list.get(i)),16);
                    i = i + 1 + lengthCompression;//该字段占1字节，故加1
                    

                    //Extensions length
                    int lengthExtensions = Integer.parseInt(list.get(i)+list.get(i+1),16);
                    i = i + 2;//该字段占2个字节，故加2
                    
                    //循环解析Extensions
                    for(int j=0 ;j<lengthExtensions;j++) {
                        int extensionType = Integer.parseInt(list.get(i)+list.get(i+1),16);
                    	if(extensionType != 0) {
                    		i=i+2;
                            int extensionLength = Integer.parseInt(list.get(i)+list.get(i+1),16);
                            i = i+2+extensionLength;
                        }else {//找到service_name，0x0000
                        	i=i+2;
                    		int extensionLength = Integer.parseInt(list.get(i)+list.get(i+1),16);
                    		i=i+2;
                    		list1.addAll(list.subList(i+5, i + extensionLength));//这里的5是server name list/2，service name type/1，service name length/2，字段长度之和
                    		break;
                        }
                    }
                    break;

                }
            }

            logger.info(list1);

            //将该字段解析
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < list1.size(); i++){
                int decimal = Integer.parseInt(list1.get(i), 16);
                sb.append((char) decimal);
            }
            sb.toString();
            logger.debug(sb);

            //logger.info("-------------ascii + "+convertHexToString(printHexBinary(data)));
            return sb;
        }

        return null;

    }

    public static String printHexBinary(byte[] data) {
        char[] hexCode = "0123456789ABCDEF".toCharArray();
        StringBuilder r = new StringBuilder(data.length * 2);
        for (byte b : data) {
            r.append(hexCode[(b >> 4) & 0xF]);
            r.append(hexCode[(b & 0xF)]);
            r.append(",");
        }
        return r.toString();
    }

}
